//
// Created by xzl01 on 2022/4/15.
//
#include <QCalendarWidget>
#include "../include/XCalendar.h"
#include <QDebug>

XCalendar::XCalendar(QWidget *parent)
		: QCalendarWidget(parent)
{
	initControl();
}

XCalendar::~XCalendar()
{

}

void XCalendar::initControl()
{
	setLocale(QLocale(QLocale::Chinese));//设置中文
	setNavigationBarVisible(false);//关闭导航条
	setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);//去掉列表头
	setHorizontalHeaderFormat(QCalendarWidget::LongDayNames);
	setSelectionMode(QCalendarWidget::SingleSelection);//单选

	QTextCharFormat format;
	format.setForeground(QColor(51, 51, 51));
	format.setBackground(QColor(247, 247, 247));
	format.setFontFamily("Microsoft YaHei");//后期这里要改掉 可能有版权风险 TODO:需要切换字体
	format.setFontPointSize(9);
	format.setFontWeight(QFont::Medium);
	setWeekdayTextFormat(Qt::Saturday, format);
	setWeekdayTextFormat(Qt::Sunday, format);

	initTopWidget();
	//connect(this, &QCalendarWidget::currentPageChanged, [this](int year, int month){
		//setDataLabelText(year, month);});




}

void XCalendar::initTopWidget()
{
	QWidget* topWidget = new QWidget(this);
	topWidget->setObjectName("CalendarTopWidget");
	topWidget->setFixedHeight(40);
	topWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);

	QHBoxLayout* hboxLayout = new QHBoxLayout();//水平布局
	hboxLayout->setContentsMargins(12,0,12,0);//设置边距
	hboxLayout->setSpacing(4);//各个控件之间的上下边距

	leftMonthBtn = new QPushButton(this);
	leftMonthBtn->setText("<");//左右切换图标 TODO：需要更改为图片

	rightMonthBtn = new QPushButton(this);
	leftMonthBtn->setText(">");
	dataLabel =  new QLabel;

	leftMonthBtn->setObjectName("XCalendarLeftButton");
	rightMonthBtn->setObjectName("XCalendarRightButton");
	dataLabel->setObjectName("XCalendarDataLabel");
	//btn大小设置
	leftMonthBtn->setFixedSize(16,16);
	rightMonthBtn->setFixedSize(16,16);
	//布局设置
	hboxLayout->addStretch();//设计师中的弹簧
	hboxLayout->addWidget(leftMonthBtn);
	hboxLayout->addWidget(dataLabel);
	hboxLayout->addWidget(rightMonthBtn);
	hboxLayout->addStretch();

	topWidget->setLayout(hboxLayout);

	QVBoxLayout* vboxlayout = new QVBoxLayout();//垂直布局
	vboxlayout->insertWidget(0,topWidget);

	connect(leftMonthBtn,&QPushButton::clicked,this,&XCalendar::onBtnClicked);
	connect(rightMonthBtn,&QPushButton::clicked,this,&XCalendar::onBtnClicked);

	setDataLabelText(selectedDate().year(),selectedDate().month());


}



void XCalendar::onBtnClicked()//重写鼠标点击事件
{
	QPushButton *senderBtn = qobject_cast<QPushButton *>(sender());
	if (senderBtn == leftMonthBtn)
	{
		showPreviousMonth();
	}
	else if (senderBtn == rightMonthBtn)
	{
		showNextMonth();
	}
}

void XCalendar::paintCell(QPainter *painter, const QRect &rect, const QDate &date) const
{
	if(date==selectedDate())
	{
		painter->save();
		painter->setRenderHint(QPainter::Antialiasing);
		painter->setPen(Qt::NoPen);
		painter->setBrush(QColor(118, 178, 224));
		painter->drawEllipse(QRect(rect.x()+rect.width()/2-10, rect.y() +
								rect.height()/2-10, 20, 20));
		painter->setPen(QColor(255, 255, 255));
		painter->drawText(rect, Qt::AlignCenter, QString::number(date.day()));
		painter->restore();


	}
	else if (date==QDate::currentDate())
	{
		painter->save();
		painter->setRenderHint(QPainter::Antialiasing);
		painter->setPen(QColor(118, 178, 224));

		painter->drawText(rect, Qt::AlignCenter, QString::number(date.day()));
		painter->restore();


	}
	else if (date < minimumDate() || date > maximumDate())
	{

		qDebug()<< minimumDate()<<maximumDate();
		painter->save();
		painter->setRenderHint(QPainter::Antialiasing);
		painter->setPen(Qt::NoPen);
		painter->setBrush(QColor(249, 249, 249));

		painter->drawRect(rect.x(), rect.y() + 3, rect.width(), rect.height() - 6);

		painter->setPen(QColor(255,0,0));

		painter->drawText(rect, Qt::AlignCenter, QString::number(date.day()));
		painter->restore();
	}

	else{
		QCalendarWidget::paintCell(painter, rect, date);
	}
}

void XCalendar::setDataLabelText(int year, int month)
{
	dataLabel->setText(QStringLiteral("%1年%2月").arg(year).arg(month));
}




